//package com.mmall.util;
//
//import com.alipay.api.domain.ExtendParams;
//import com.alipay.api.domain.GoodsDetail;
//import com.alipay.api.response.AlipayTradePrecreateResponse;
//import com.alipay.demo.trade.config.Configs;
//import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
//import com.alipay.demo.trade.model.result.AlipayF2FPayResult;
//import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
//import com.alipay.demo.trade.service.AlipayTradeService;
//import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
//import com.alipay.demo.trade.utils.ZxingUtils;
//import com.google.common.collect.Lists;
//import com.google.common.collect.Maps;
//import com.mmall.common.ServerResponse;
//import com.mmall.dao.OrderItemMapper;
//import com.mmall.dao.OrderMapper;
//import com.mmall.pojo.Order;
//import com.mmall.pojo.OrderItem;
//import org.omg.PortableServer.Servant;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by yangtianrui on 17/7/12.
// */
//public class Pay {
//
//    @Autowired
//    OrderMapper orderMapper;
//    @Autowired
//    OrderItemMapper orderItemMapper;
//    private static  final Logger logger = LoggerFactory.getLogger(Pay.class);
//     public ServerResponse getPay(Long  orderNo,Integer userId,String path)
//     {
//         Order order = orderMapper.selectByUserIdAndOrderNo(userId,orderNo);
//         if(order == null)
//         {
//            return ServerResponse.createByErrorMessage("该订单不存在");
//         }
//         Map<String ,String> resultMap;
//         resultMap = new HashMap<String,String>();
//
//         resultMap.put("orderNo",order.getOrderNo().toString());
//         String outTradeNO = order.getOrderNo().toString();//  获取订单号
//         String subject = new StringBuilder("扫码支付").append("订单号").append(order.getOrderNo()).toString();
//         String totalAmount = order.getPayment().toString();// 订单金额
//         String undiscountAmount = "0";
//         String selllerId ="";//  卖家的支付宝ID 默认为空时 时商户的pid
//         String body = new StringBuilder("订单").append(order.getOrderNo()).append(totalAmount).append("元").toString();
//         String storeId = "test_store_id";//商户的门店编号 通过门店号和商家后台可以配置到准确的门店打折信息。
//         String operateId = "test_oprate_id";
//         ExtendParams extendParams = new ExtendParams();//  业务扩展字段
//         extendParams.setSysServiceProviderId("2088100200300400500");
//         String timeoutExppress = "120";
//         List<com.alipay.demo.trade.model.GoodsDetail> goodsDetailList = new ArrayList<com.alipay.demo.trade.model.GoodsDetail>()
//         List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
//         for(OrderItem orderItem :orderItemList)
//         {//    构建商品详情
//             com.alipay.demo.trade.model.GoodsDetail goodsDetail  = com.alipay.demo.trade.model.GoodsDetail
//                     .newInstance(orderItem.getProductId().toString(),orderItem.getProductName(),BigDecimalUtil.mul(orderItem.getCurrentUnitPrice().doubleValue(),new Double(100).doubleValue()).longValue(),orderItem.getQuantity());
//             goodsDetailList.add(goodsDetail);
//         }
//         // 开始创建扫码支付请求builder 设置请求参数
//         AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
//                 .setSubject(subject).setTotalAmount(totalAmount).setOutTradeNo(outTradeNO)
//                 .setUndiscountableAmount(undiscountAmount).setSellerId(selllerId).setBody(body)
//                 .setOperatorId(operateId).setStoreId(storeId).setExtendParams(extendParams)
//                 .setTimeoutExpress(timeoutExppress)
//                 .setNotifyUrl("")
//                 .setGoodsDetailList(goodsDetailList);
////        一定要在AlipayTradeService之前调用Configs.init
//         Configs.init("zfbinfo.propertiesproperties");
//         AlipayTradeService tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();
//         AlipayF2FPrecreateResult result = tradeService.tradePrecreate(builder);
//        switch (result.getTradeStatus()) {
//            case SUCCESS:
//                logger.info("支付宝预下单成功");
//                AlipayTradePrecreateResponse response = result.getResponse();
//                dumpResponse(response);
//
//                File folder = new File(path);
//                if (folder.exists()) {
//                    folder.setWritable(true);
//                    folder.mkdirs();
//
//                }
//                String qrpath = String.format(path + "/" + "qr-%s.png", response.getOutTradeNo());
//                String qrFileName = String.format("qr-%s.png,"response.getOutTradeNo());
//                ZxingUtils.getQRCodeImge(response.getQrCode(), 256, qrpath);
//
//                File targetFile = new File(path, qrFileName);
//                List<File> targetList = new ArrayList<File>();
//                targetList.add(targetFile);
//                try {
//                    FTPUtil.uploadFile(targetList);
//                    FTPUtil.uploadFile(Lists.<File>newArrayList(targetFile));
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                String qrurl = PropertiesUtil.getProperty("") + targetFile.getName();
//                resultMap.put("qrurl", qrurl);
//                return ServerResponse.createBySuccess(result);
//            case FAILED:
//
//
//        }
//
//     }
//
//    private void dumpResponse(AlipayTradePrecreateResponse response) {
//
//
//    }
//
//
//}
